<script>
  import { goto } from '$app/navigation';
  import * as api from '$lib/apiServer';

  import LogIn from '$lib/components/LogIn.svelte';

  let { data } = $props();

  async function acceptInvite(inviteId, username, password) {
    const response = await api.acceptInvite(inviteId, username, password);
    if (200 <= response.status && response.status < 300) {
      await goto('/');
    }
  }
</script>

{#await data.invite}
  <div class="invite-text">
    <p>Loading invitation…</p>
  </div>
{:then invite}
  <div class="invite-text">
    <p>Hi there! {invite.issuer} invites you to the conversation.</p>
  </div>
  <LogIn logIn={async (username, password) => acceptInvite(invite.id, username, password)} />
{/await}
